<template>
  <div class="boxs">
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <!-- card title -->
      <div slot="header">
        <!-- 用一个行列布局 -->
        <el-row>
          <el-col :span="20">
            <span>{{ company }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 1.添加一级部门=》没有父
                    注意： openAdd函数要传null，代表没有父节点
                     -->
                    <el-dropdown-item @click.native="openAdd(null)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </div>
      <!-- card body -->
      <el-tree :data="data" :props="defaultProps" :default-expand-all="true" @node-click="handleNodeClick">
        <template #default="{ data }">
          <!-- 节点的自定义html结构
          data 是el-tree提供的当前节点的数据
          -->
          <!-- <h3>{{ data }}</h3> -->
          <el-row style="width: 100%">
            <!-- 1. 左边：部门名 -->
            <el-col :span="20">
              <span>{{ data.name }}</span>
            </el-col>
            <!-- 2. 右边：负责人和操作菜单 -->
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <el-col>
                  <!-- 下拉菜单 element -->
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <!-- 下拉菜单 -->
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="openAdd(data,1)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="openAdd(data,2)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="delDept(data)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </el-card>
    <!-- 新增部门弹层子组件 -->
    <AddDept ref="dept" :show="show" :all-list="allList" :parent-node="parentNode" @refresh-list="getDeptList" @close="closeDialog" />
  </div>
</template>

<script>
import { getDepartmentsApi, delDepartmentsApi } from '@/api/departments'
import { listToTreeData } from '@/utils'
// 导入新增部门弹层组件
import AddDept from './components/add-dept.vue'
export default {
  // 给组件起个名
  name: 'Departments',
  components: {
    AddDept
  },
  data() {
    return {
      // 当前添加父部门数据
      parentNode: null,
      // 控制新增部门弹层显隐
      show: false,
      // 公司信息
      company: '',
      allList: [],
      data: [
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDeptList()
  },
  methods: {
    // 弹层关闭会执行
    closeDialog() {
      this.show = false
    },
    // 打开新增 | 编辑部门弹层
    // 1.点击头部公司的添加=》顶级(一级)部门=》没有父
    // 2.点击树形组件中某个节点添加=》添加的子部门=》后台知道父部门是谁
    openAdd(parentNode, type) {
      this.show = true
      // 存储父部们数据
      // console.log(parentNode)
      this.parentNode = parentNode
      if (type === 2) {
        this.$refs.dept.getDept(parentNode.id)
      }
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 删除当前部门数据
    delDept(data) {
      // console.log('当前删除的部门', data)
      this.$confirm(`确定删除: ${data.name}吗？`).then(async() => {
        if (data.children && data.children.length > 0) {
          return this.$message.error('当前部门下有子部门,不能删除！')
        }
        // eslint-disable-next-line no-undef
        await delDepartmentsApi(data.id)
        this.$message.success('删除成功')
        // 刷新列表
        this.getDeptList()
      }).catch(() => {
        // 点取消
      })
    },
    // 获取组织架构部门数据
    async getDeptList() {
      const { companyName, depts } = await getDepartmentsApi()
      // console.log('部门树形数据', companyName, depts)
      this.company = companyName
      this.allList = depts
      //   转换树形数据
      this.data = listToTreeData(depts)
    }

  }
}
</script>

<style lang="scss" scoped>
//::v-deep 深度作用选择符=》替换修改饿了么组件默认样式
::v-deep .el-tree-node__content{
  margin: 2px 0;
 }
</style>
